package cn.dida.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dida.dao.QrcodeDao;
import cn.dida.model.Pageable;
import cn.dida.model.UserSession;
import cn.dida.service.QrcodeService;
import cn.dida.util.NumberConvetUtil;
/**
 * 二维码接口实现类
 * @author weiwei
 *
 */
@Service
public class QrcodeServiceImpl implements QrcodeService {
	@Autowired
	private QrcodeDao qrcodeDao;

	@Override
	public Pageable<Map<String, Object>> queryAllQrcodeByCompnayID(Integer companyId, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryAllQrcodeByCompnayID(companyId, paramMap, page);
	}

	@Transactional
	@Override
	public synchronized Integer insertQrcode(String qcname, Byte type, Integer nubs, UserSession userSession) {
		// TODO Auto-generated method stub

		Long nowlong = Calendar.getInstance().getTimeInMillis();

		String idadmin = userSession.getId() + "_" + userSession.getAccount();

		int qrcode_base_id = 0;
		try {
			qrcode_base_id = qrcodeDao.insertQrcodeBase(qcname, type, nubs, userSession, nowlong, idadmin);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			String errmsg = e.getMessage();
			if (errmsg.indexOf("Duplicate entry") != -1 && errmsg.indexOf("for key") != -1) {
				return -1;
			}

		}

		if (qrcode_base_id > 0) {
			int result = qrcodeDao.insertQrcodeDetail(qrcode_base_id, nubs,userSession.getCompanyId());
			//System.out.println("insertQrcodeDetail  == >"+result);
			List<Map<String,Object>> results = qrcodeDao.queryQrcodeBaseID(qrcode_base_id,userSession.getCompanyId());
			
			if(results!=null&&results.size()>0){
				int resut=qrcodeDao.insertLogresQrcode(results,userSession.getCompanyId());
				//System.out.println("insertLogresQrcode  == >"+resut);
			}
			Map<String, Object> rmap = qrcodeDao.selectMinIDAndMaxIDByQrcodeBaseID(qrcode_base_id,userSession.getCompanyId());
			if (null != rmap) {
				Long start_no = NumberUtils.toLong(rmap.get("minid").toString());
				Long end_no = NumberUtils.toLong(rmap.get("maxid").toString());
				qrcodeDao.updateQrcodeBH(qrcode_base_id, start_no, end_no);
			}
			
		}

		return 1;
	}

	@Override
	public Pageable<Map<String, Object>> queryAllQrcodeGroupByCompnayID(Integer companyId, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryAllQrcodeGroupByCompnayID(companyId, paramMap, page);
	}

	@Transactional
	@Override
	public synchronized Integer insertQrcodeGroup(String qcname, Integer pbcId, Integer nubs, UserSession userSession) {
		// TODO Auto-generated method stub

		Long nowlong = Calendar.getInstance().getTimeInMillis();

		String idadmin = userSession.getId() + "_" + userSession.getAccount();

		int qrcode_base_group_id = 0;
		try {
			qrcode_base_group_id = qrcodeDao.insertQrcodeBase(qcname, pbcId, nubs, userSession, nowlong, idadmin);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			String errmsg = e.getMessage();
			if (errmsg.indexOf("Duplicate entry") != -1 && errmsg.indexOf("") != -1) {
				return -1;
			}

		}

		if (qrcode_base_group_id > 0) {
			int result = qrcodeDao.insertQrcodeDetailGroup(qrcode_base_group_id, nubs, pbcId,userSession.getCompanyId());
			//查询此类二维码id
			List<Map<String,Object>> results = qrcodeDao.queryQrcodeBaseID(qrcode_base_group_id,userSession.getCompanyId());
			//将id和生成的物流码编码插入在相对应的物流码关联表中
			if(results != null && results.size() > 0){
				int resut=qrcodeDao.insertLogresQrcode(results,userSession.getCompanyId());
				System.out.println("insertLogresQrcode  == >"+resut);
			}
			//查询二维码瓶箱关联id
			List<Map<String,Object>> contactresults = qrcodeDao.queryQrcodeBasePIDBId(qrcode_base_group_id,userSession.getCompanyId());
			//将瓶箱id插入在lgs_res_qrbind表中
			if(contactresults != null && contactresults.size() > 0){
				int resut=qrcodeDao.insertLogresQrcodeQrbing(contactresults,userSession);
				System.out.println("insertLogresQrcodeQrbing  == >"+resut);
			}
			Map<String, Object> rmap = qrcodeDao.selectMinIDAndMaxIDByQrcodeBaseGroupID(qrcode_base_group_id,userSession.getCompanyId());
			if (null != rmap) {
				Long start_no = NumberUtils.toLong(rmap.get("minid").toString());
				Long end_no = NumberUtils.toLong(rmap.get("maxid").toString());
				Long g_start_no = NumberUtils.toLong(rmap.get("gminid").toString());
				Long g_end_no = NumberUtils.toLong(rmap.get("gmaxid").toString());
				qrcodeDao.updateQrcodeGroupBH(qrcode_base_group_id, start_no, end_no, g_start_no, g_end_no);
			}

		}

		return 1;

	}

	@Override
	public Pageable<Map<String, Object>> queryAllQrcodeProdcutCompnayID(Integer companyId, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryAllQrcodeProdcutCompnayID(companyId, paramMap, page);
	}

	@Override
	public List<Map<String, Object>> queryProductName(String pname, Integer limit, Integer company_id) {
		// TODO Auto-generated method stub

		return qrcodeDao.queryProductName(pname, limit, company_id);
	}

	@Override
	public List<Map<String, Object>> queryQrcodeName(String qcname, Integer type, Integer limit, Integer companyId) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeName(qcname, type, limit, companyId);
	}

	@Override
	public Map<String, Object> queryQrcodeNoBangdingCount(Integer id, Integer type, Integer companyid) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeNoBangdingCount(id, type,companyid);
	}

	@Transactional
	@Override
	public synchronized Integer insertQrcodeProduct(Map<String, Object> paramMap, Integer type, UserSession userSession) {
		// TODO Auto-generated method stub
		String pnames = paramMap.get("pname").toString();
		String batch_nub = "";
		if(paramMap.containsKey("batch_nub")){
			batch_nub = (String)paramMap.get("batch_nub");
		}
		
		Integer pid =0;
		if(paramMap.get("pid")!=null){
			String pids = paramMap.get("pid").toString();
		    pid = NumberUtils.toInt(pids, 0);
		}else{
		    pid = NumberUtils.toInt(pnames.split("_")[1], 0);
		}
		if (pid <= 0) {
			return -1;
		}
		String qcnames = paramMap.get("qcname").toString();
		Long produce_date = NumberUtils.toLong(paramMap.get("produce_date").toString(), 0L);
		Long qcid = NumberUtils.toLong(qcnames.substring(qcnames.lastIndexOf("_") + 1), 0L);
		if (qcid <= 0L) {
			return -2;
		}

		Long pms = NumberUtils.toLong(paramMap.get("pms").toString(), 0L);

		Long pme = NumberUtils.toLong(paramMap.get("pme").toString(), 0L);

		if (pms < 0L || pme < 0L || pms > pme) {
			return -3;
		}

		int rn = 1;

		if (1 == type || 2 == type) {

			Integer n = qrcodeDao.selectIsExistBandingOfQrcode(qcid, pms, pme);

			if (n > 0) {
				return -5;
			} else {
				// 插入绑定关系
				Integer ppqid = qrcodeDao.insertQrcodeProduct(pid, qcid, type, pms, pme, userSession,batch_nub,produce_date,0);
				// 更新绑定详情
				qrcodeDao.updateQrcodeDetailOfProduct(pid, qcid, ppqid, pms, pme,userSession.getCompanyId());
				// 更新二维码主表绑定数量
				qrcodeDao.updateQrcodeBaseOfBandings(qcid, pms, pme);
			}

		} else if (3 == type) {

			Integer n = qrcodeDao.selectIsExistBandingOfQrcodeGroup(qcid, pms, pme);

			if (n > 0) {
				return -5;
			} else {

				Long xms = NumberUtils.toLong(paramMap.get("xms").toString(), 0L);

				Long xme = NumberUtils.toLong(paramMap.get("xme").toString(), 0L);

				if (xms < 0L || xme < 0L || xms > xme) {
					return -4;
				}
				// 插入绑定关系
				Integer ppqid = qrcodeDao.insertQrcodeGroupProduct(pid, qcid, type, pms, pme, xms, xme, userSession,batch_nub,produce_date);
				// 更新绑定详情
				qrcodeDao.updateQrcodeDetailGroupOfProduct(pid, qcid, ppqid, pms, pme, xms, xme,userSession.getCompanyId());
				// 更新二维码主表绑定数量
				qrcodeDao.updateQrcodeBaseGroupOfBandings(qcid, pms, pme, xms, xme);
			}
		}

		return rn;
	}

	@Override
	public Long queryQrcodeProductResetEndNoByXme(Integer id,Integer companyid) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeProductResetEndNoByXme(id,companyid);
	}

	@Transactional
	@Override
	public Integer deleteQrcodeProduct(Integer id, UserSession userSession, Integer type) {
		// TODO Auto-generated method stub
		qrcodeDao.deleteQrcodeProductById(id, userSession);
		if (1 == type || 2 == type) {
			qrcodeDao.updateQrcodeDetaiForNoBangding(id, userSession);
		} else if (3 == type) {
			qrcodeDao.updateQrcodeDetaiGroupForNoBangding(id, userSession);
		}

		return 1;
	}

	@Override
	public Pageable<Map<String, Object>> queryQrcodeDetailByID(Integer companyId, String id,String proName, String qrId, Integer state,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeDetailByID(companyId, id, proName,qrId,state,page);
	}

	@Override
	public Pageable<Map<String, Object>> queryQrcodeBindingByID(Integer companyId, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeBindingByID(companyId, paramMap, page);
	}

	@Override
	public Pageable<Map<String, Object>> queryQrcodegroupBindingByID(Integer companyId, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodegroupBindingByID(companyId, paramMap, page);
	}

	@Override
	public Pageable<Map<String, Object>> queryQrcodegroupDetailByID(Integer companyId,String  id,String proName, String qrId, Integer state,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodegroupDetailByID(companyId, id,proName,qrId,state, page);
	}

	@Transactional
	@Override
	public Integer updateQrcodeGroup(Integer id, String pname,String batch_nub, Integer type,Integer pid,
									 String produce_date,UserSession userSession) {
		// TODO Auto-generated method stub
		if (pid <= 0) {
			return -1;
		}
		if (1 == type || 2 == type) {
			qrcodeDao.updateQrcodeProduct(id, pid,batch_nub,produce_date, userSession);
			qrcodeDao.updateQrcodeDetailOfProductForPid(id, pid, userSession);
		} else if (3 == type) {
			qrcodeDao.updateQrcodeProduct(id, pid,batch_nub,produce_date, userSession);
			qrcodeDao.updateQrcodeDetailGroupOfProductForPid(id, pid, userSession);
		}

		return 1;
	}

	@Override
	public List<Map<String, Object>> queryQrcodeInfoForDownload(Integer id,Integer isAutoDraw, UserSession userSession) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeInfoForDownload(id, isAutoDraw, userSession);
	}

	@Override
	public List<Map<String, Object>> queryQrcodeGroupInfoForDownload(Integer id, UserSession userSession) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodeGroupInfoForDownload(id, userSession);
	}

	@Transactional
	@Override
	public void updateDownlaodState(Integer id) {
		// TODO Auto-generated method stub
		qrcodeDao.updateDownlaodState(id);
	}
	@Transactional
	@Override
	public void updateGroupDownlaodState(Integer id) {
		// TODO Auto-generated method stub
		qrcodeDao.updateGroupDownlaodState(id);
	}

	@Override
	public List<Map<String, Object>> queryAllQrcodeByCompnayIDForExport(Integer companyId,
			Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryAllQrcodeByCompnayIDForExport(companyId, paramMap);
	}

	@Override
	public List<Map<String, Object>> queryAllQrcodeProdcutCompnayIDForExport(Integer companyId,
			Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryAllQrcodeProdcutCompnayIDForExport(companyId, paramMap);
	}

	@Override
	public List<Map<String, Object>> queryAllQrcodeGroupByCompnayIDForExport(Integer companyId,
			Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryAllQrcodeGroupByCompnayIDForExport(companyId, paramMap);
	}

	@Override
	public Integer updateQrcodeProductActSta(Integer id, UserSession userSession, Integer type) {
		// TODO Auto-generated method stub
		return qrcodeDao.updateQrcodeProductActSta(id,type,userSession);
	}
	
	
	@Override
	public Map<String, Object> queryQrcodePbcchangproduct(Integer id) {
		// TODO Auto-generated method stub
		return qrcodeDao.queryQrcodePbcchangproduct(id);
	}
	

}
